package com.ruoyi.mng.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Supplier;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.mng.domain.HzBusi;
import com.ruoyi.mng.domain.HzChaChannel;
import com.ruoyi.mng.domain.HzMerMerchant;
import com.ruoyi.mng.service.IHzBusiService;
import com.ruoyi.mng.service.IHzChaChannelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mng.domain.HzChaBusiR;
import com.ruoyi.mng.service.IHzChaBusiRService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 渠道业务关系Controller
 *
 * @author chenqiang
 * @date 2023-04-28
 */
@Controller
@RequestMapping("/mng/chabusir")
public class HzChaBusiRController extends BaseController {
    private String prefix = "mng/chabusir";

    @Autowired
    private IHzChaBusiRService hzChaBusiRService;
    @Resource
    private IHzChaChannelService hzChaChannelService;
    @Resource
    private IHzBusiService hzBusiService;

    @RequiresPermissions("mng:chabusir:view")
    @GetMapping()
    public String chabusir(ModelMap mmap) {
        Supplier<HzChaChannel> channelSupplier = HzChaChannel::new;
        List<HzChaChannel> hzChaChannelList = hzChaChannelService.selectHzChaChannelList(channelSupplier.get());
        ArrayList<HashMap<String, Object>> channelList = new ArrayList<>();
        hzChaChannelList.forEach(chaChannel -> {
            HashMap<String, Object> channelMap = new HashMap<>();
            channelMap.put("channelId", chaChannel.getChannelId());
            channelMap.put("channelName", chaChannel.getChannelName());
            channelList.add(channelMap);
        });
        mmap.put("channelList", channelList);
        return prefix + "/chabusir";
    }

    /**
     * 查询渠道业务关系列表
     */
    @RequiresPermissions("mng:chabusir:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HzChaBusiR hzChaBusiR) {
        startPage();
        List<HzChaBusiR> list = hzChaBusiRService.selectHzChaBusiRList(hzChaBusiR);
        return getDataTable(list);
    }

    /**
     * 导出渠道业务关系列表
     */
    @RequiresPermissions("mng:chabusir:export")
    @Log(title = "渠道业务关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HzChaBusiR hzChaBusiR) {
        List<HzChaBusiR> list = hzChaBusiRService.selectHzChaBusiRList(hzChaBusiR);
        ExcelUtil<HzChaBusiR> util = new ExcelUtil<HzChaBusiR>(HzChaBusiR.class);
        return util.exportExcel(list, "渠道业务关系数据");
    }

    /**
     * 新增渠道业务关系
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        Supplier<HzChaChannel> channelSupplier = HzChaChannel::new;
        List<HzChaChannel> hzChaChannelList = hzChaChannelService.selectHzChaChannelList(channelSupplier.get());
        ArrayList<HashMap<String, Object>> channelList = new ArrayList<>();
        hzChaChannelList.forEach(chaChannel -> {
            HashMap<String, Object> channelMap = new HashMap<>();
            channelMap.put("channelId", chaChannel.getChannelId());
            channelMap.put("channelName", chaChannel.getChannelName());
            channelList.add(channelMap);
        });


        Supplier<HzBusi> busiSupplier = HzBusi::new;
        List<HzBusi> hzBusiList = hzBusiService.selectHzBusiList(busiSupplier.get());
        ArrayList<HashMap<String, String>> busiList = new ArrayList<>();
        hzBusiList.forEach(busi -> {
            HashMap<String, String> busiMap = new HashMap<>();
            busiMap.put("id", busi.getBusiId().toString());
            busiMap.put("name", busi.getBusiName());
            busiList.add(busiMap);
        });
        mmap.put("busiList", busiList);
        mmap.put("channelList", channelList);
        return prefix + "/add";
    }

    /**
     * 新增保存渠道业务关系
     */
    @RequiresPermissions("mng:chabusir:add")
    @Log(title = "渠道业务关系", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HzChaBusiR hzChaBusiR) {
        //查询渠道业务关系是否已经存在
        List<HzChaBusiR> hzChaBusiRList = hzChaBusiRService.selectHzChaBusiRList(hzChaBusiR);
        if (null != hzChaBusiRList && hzChaBusiRList.size() > 0) {
            return AjaxResult.error("已经存在该渠道业务关系");
        }
        return toAjax(hzChaBusiRService.insertHzChaBusiR(hzChaBusiR));
    }

    /**
     * 修改渠道业务关系
     */
    @RequiresPermissions("mng:chabusir:edit")
    @GetMapping("/edit/{rId}")
    public String edit(@PathVariable("rId") Long rId, ModelMap mmap) {
        HzChaBusiR hzChaBusiR = hzChaBusiRService.selectHzChaBusiRByRId(rId);
        mmap.put("hzChaBusiR", hzChaBusiR);
        return prefix + "/edit";
    }

    /**
     * 修改保存渠道业务关系
     */
    @RequiresPermissions("mng:chabusir:edit")
    @Log(title = "渠道业务关系", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HzChaBusiR hzChaBusiR) {
        return toAjax(hzChaBusiRService.updateHzChaBusiR(hzChaBusiR));
    }

    /**
     * 删除渠道业务关系
     */
    @RequiresPermissions("mng:chabusir:remove")
    @Log(title = "渠道业务关系", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hzChaBusiRService.deleteHzChaBusiRByRIds(ids));
    }
}
